*** LOADING DATA FILE
cd "C:\Users\stinhess\Dropbox\Delte_mapper\2. ClassIDscandi med Rune og Peter\Data\NNES" // replace with your directory
use "replicationfile_NO.dta"

*** ANALYSES 
cd "C:\Users\stinhess\Dropbox\Delte_mapper\2. ClassIDscandi med Rune og Peter\Data\Final figures" // replace with your directory

* Creating the Norwegianpart of Figure 1: Distribution of class identity over time
preserve
tabulate classid3, generate(var)
collapse (sum) var1 var2 var3 , by(year)
gen p1 = var1 / (var1 +var2 +var3)
gen p2 = (var1+var2) / (var1 +var2 +var3)
*gen p3 = (var1+var2+var3) / (var1 +var2 +var3)
gen p7 = 1
gen zero = 0

twoway rarea zero p1 year, color(gs0) || rarea p1 p2 year, color(gs14) || rarea  p2 p7 year, color(gs8) legend(order(1 "Working class ID" 2 "No class ID" 3 "Middle class ID" )) ytitle(Percent) xla(1977 1981 1989 1993 2001 2021, angle(45)) xtitle("") title("Norway") 
restore 

//graph save "classdist3NO.gph", replace
//graph export "classdist3NO.png", replace


* Creating the Norwegian part of Figure 2: Class identification and attitudes to redistribution 
* For the main paper: 2 classes, multivariate 
eststo redistno: reg tax i.classid2##i.year i.agekat i.gender i.edu2 i.class
margins year#classid2
marginsplot, title("Norway") xtitle("") ytitle("Redistribution") recastci(rarea) xlabel(, angle(45)) ciopt(color(%20)) // saving(eclr2multiNO.gph, replace)
//graph export eclr2multiNO.png, replace


*Creating the Norwegian part of Figure 3: Class identification and attitudes to immigration
* For the main paper: Immigration, 2-class id, multivariate
eststo immigno: reg cultthreat i.classid2##i.year i.agekat i.gender i.edu2 i.class
margins year#classid2
marginsplot,  title("Norway") xtitle("") ytitle("Immigration attitudes") recastci(rarea) xlabel(, angle(45)) ylabel(.3(.1).8) ciopt(color(%20)) // saving(img2multiNO.gph, replace)
//graph export img2multiNO.png, replace

* Creating Figrue 5: Class id and vote choice

* For the main paper: Multivariate, 2 classes
eststo partyno: mlogit party4 i.classid2##ib2022.year i.agekat i.gender i.edu2 i.class 

margins year#classid2, predict(outcome(1)) // Old left plot
marginsplot, title("Old Left") xtitle("") ytitle("% support") xlabel(, angle(45)) recastci(rarea) ylab(0(.1).8) legend(row(1)) ciopt(color(%20)) saving(oldleft2multiNO.gph, replace) 
//graph export oldleft2multiNO.png, replace

margins year#classid2, predict(outcome(2)) // New left plot
marginsplot, title("New Left") xtitle("") ytitle("% support") xlabel(, angle(45)) recastci(rarea) ylab(0(.1).3) legend(row(1)) ciopt(color(%20)) saving(newleft2multiNO.gph, replace) 
//graph export newleft2multiNO.png, replace

margins year#classid2, predict(outcome(3)) // Old right plot
marginsplot, title("Old Right") xtitle("") ytitle("% support") xlabel(, angle(45)) recastci(rarea) ylab(0(.1).8) ciopt(color(%20)) saving(oldright2multiNO.gph, replace) 
//graph export oldright2multiNO.png, replace

margins year#classid2, predict(outcome(4)) // New right plot
marginsplot, title("New Right") xtitle("") ytitle("% support") xlabel(, angle(45)) recastci(rarea)  ylab(0 (.1) .3) ciopt(color(%20)) saving(newright2multiNO.gph, replace)
//graph export newright2multiNO.png, replace

graph combine oldleft2multiNO.gph  oldright2multiNO.gph newleft2multiNO.gph newright2multiNO.gph 
//graph save party2multiNO.gph, replace
//graph export party2multiNO.png, replace


***Part 2: Analyses of party polarization interaction

merge m:1 year using "NOmanifest.dta"
drop if _merge<3

xtset year

gen ecopos= ecoposno
gen autlib= autlibno

**Norwegian results, Models in main paper, Table 2

eststo mino: xtmlogit party4 i.classid2##c.ecopos i.gender i.edu2 i.agekat i.class, re
estimates save "m1no", replace
test 1.classid2#c.ecopos
eststo m2no: xtmlogit party4 i.classid2##c.autlib i.gender i.edu2 i.agekat i.class, re
estimates save "m2no", replace
test 1.classid2#c.autlib
eststo m3no: xtmlogit party4 i.classid2##c.ecopos i.classid2##c.autlib i.gender i.edu2 i.agekat i.class, re
estimates save "m3no", replace
test 1.classid2#c.ecopos
test 1.classid2#c.autlib

reg party4 i.classid2 c.ecopos c.autlib i.gender i.edu2 i.agekat i.class, r
estat vif






